package demo.DataStrucAndAlgo.SingletonPattern;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/7/30-07-30-9:04
 * @Description：demo.DataStrucAndAlgo.SingletonPattern   懒汉式单例模式
 */
/*
懒汉式单例模式
    懒初始化
    多线程不安全

 */
public class Singleton {
    private static Singleton instance;

    private Singleton(){}

    //public static Singleton getInstance(){
    public static synchronized Singleton getInstance(){          //加锁，保证多线程安全
        if(instance == null){
            instance = new Singleton();
        }
        return instance;
    }

    public void showMessage(){
        System.out.println("hello world");
    }

    public static void main(String[] args) {
        Singleton single1 = Singleton.getInstance();
        Singleton single2 = Singleton.getInstance();
        System.out.println(single1==single2);
        single1.showMessage();
        single2.showMessage();
    }
}
